package ar.uba.fi.tecnicas.vista;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class PanelControles extends JPanel 
{
	private static final long serialVersionUID = 0;

	// Contiene los botones de las operaciones
	private JPanel botonesPanel;
	// Contiene un panel con la operacion siendo ejecutada
	private JPanel operacionPanel;
	
	private List<JButton> botones;
	private List<ActionListener> handlers;
	
	private FrameJuego frameJuego;
	
	private PanelControles self = this;
	
	public PanelControles(FrameJuego frameJuego)
	{
		super();		
		this.setLayout(new BorderLayout());
		this.setBackground(Color.GRAY);
		
		this.frameJuego = frameJuego;
		
		this.botonesPanel = new JPanel();
		this.operacionPanel = new JPanel();
		
		this.inicializarComponentes();
		this.asignarEventos();
		
		this.add(this.botonesPanel, BorderLayout.CENTER);
		this.add(this.operacionPanel, BorderLayout.PAGE_END);
	}

	private void inicializarComponentes() 
	{
		this.botones = new ArrayList<JButton>();
		
		this.botones.add(new JButton("Terreno: Adquirir"));
		this.botones.add(new JButton("Terreno: Vender"));
		this.botones.add(new JButton("Linea: Nueva"));
		this.botones.add(new JButton("Linea: Materia Prima Necesaria"));
		this.botones.add(new JButton("Linea: Primera Maquina"));
		this.botones.add(new JButton("Linea: Habilitar/Suspender"));
		this.botones.add(new JButton("Maquina: Comprar"));
		this.botones.add(new JButton("Maquina: Vender"));
		this.botones.add(new JButton("Maquina: Arreglar"));
		this.botones.add(new JButton("Maquina: Desconectar Salida"));
		this.botones.add(new JButton("Item: Mover"));
		this.botones.add(new JButton("Item: Conectar"));
		this.botones.add(new JButton("Laboratorio: Definir Porcentaje Investigacion"));
		this.botones.add(new JButton("Tiempo: Descongelar/Congelar"));
		this.botones.add(new JButton("Salir"));
		
		this.botonesPanel.setLayout(new GridLayout(this.botones.size(), 1));

		for(JButton boton : this.botones)
		{
			boton.setHorizontalAlignment(SwingConstants.LEFT);
			this.botonesPanel.add(boton);
		}
	}
	
	private void asignarEventos()
	{
		this.handlers = new ArrayList<ActionListener>();
		
		/// Terreno: Adquirir
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelAdquirirTerreno(self)); }
			}
		);
		
		/// Terreno: Vender
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ frameJuego.venderTerreno(); }
			}
		);
		
		/// Linea: Nueva
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelCrearNuevaLinea(self)); }
			}
		);
		
		/// Linea: Materia Prima Necesaria
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelAgregarMateriaPrimaNecesaria(self)); }
			}
		);
		
		/// Linea: Primera Maquina
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelElegirPrimeraMaquina(self)); }
			}
		);
		
		/// Linea: Habilitar/Suspender
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelHabilitarSuspenderLinea(self)); }
			}
		);
		
		/// Maquina: Comprar
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelComprarMaquina(self)); }
			}
		);

		/// Maquina: Vender
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelVenderMaquina(self)); }
			}
		);

		/// Maquina: Arreglar
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelArreglarMaquina(self)); }
			}
		);
		
		/// Maquina: Desconectar Salida
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelDesconectarSalidaMaquina(self)); }
			}
		);
		
		/// Item: Mover
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelMoverMaquina(self)); }
			}
		);
		
		/// Item: Conectar
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelConectar(self)); }
			}
		);
		
		/// Laboratorio: Definir Porcentaje Investigacion
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ self.cargarPanel(new PanelPorcentajeInvestigacion(self)); }
			}
		);
		
		/// Tiempo: Descongelar/Congelar
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ frameJuego.congelarDescongelarTiempo(); }
			}
		);
		
		/// Salir
		this.handlers.add(new ActionListener() {
				public void actionPerformed(ActionEvent e){ System.exit(0); }
			}
		);
		
		for(int index = 0; index < this.botones.size(); index++)
		{
			this.botones.get(index).addActionListener(this.handlers.get(index));
		}
	}

	private void cargarPanel(PanelOperacion panel)
	{
		this.operacionPanel.removeAll();
		this.operacionPanel.add(panel);
		this.frameJuego.pack();
	}

	public void descargarPanel()
	{
		this.operacionPanel.removeAll();
		this.frameJuego.pack();
		this.frameJuego.repaint(10);
	}

	public FrameJuego getFrameJuego() 
	{
		return this.frameJuego;
	}
}
